package com.atguigu.edu.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.common.EduConfig;
import org.apache.flink.api.java.tuple.Tuple2;
import redis.clients.jedis.Jedis;

import java.sql.Connection;
import java.util.List;

public class DimUtil {

    public static JSONObject getDimInfo(Connection connection, String tableName, String id) {
        //  使用的数据类型为string
        // key -> 表名+主键  value -> jsonString

        return getDimInfo(connection, tableName, new Tuple2<>("ID", id));
    }

    // 本需求中维度关联的操作都是基于id进行查询  得到的都是一行数据
    // select * from t where id = ? and name = ?
    public static JSONObject getDimInfo(Connection connection, String tableName, Tuple2<String, String>... tuples) {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from ")
                .append(EduConfig.HBASE_SCHEMA)
                .append(".")
                .append(tableName)
                .append(" where ");

        for (int i = 0; i < tuples.length; i++) {
            sql.append(tuples[i].f0)
                    .append("='")
                    .append(tuples[i].f1)
                    .append("'");

            if (i < tuples.length - 1) {
                sql.append(" and ");
            }
        }
        System.out.println(sql.toString());
        List<JSONObject> jsonObjects = PhoenixUtil.sqlQuery(connection, sql.toString(), JSONObject.class);
        if (jsonObjects.size() > 0) {
            return jsonObjects.get(0);
        } else {
            return new JSONObject();
        }
    }
}
